﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MISAPP.Classify.Service;
using System.Data;


public partial class View_WMG_WMG005 : ViewMatrix
{
    protected void Page_Load(object sender, EventArgs e)
    {
        lblResult.Text = string.Empty;
        limit();
        serviceName = "MISAPP.Classify.Service.Service_WMG005";
        criteriaList = new string[] { "Criteria" };
        Title = "台外幣定期存款分析表";
        if (!IsPostBack)
        {
            txtYM.Text = Utility.GetTaiwanYM();
            FillCriteria("GetCriteriaData", "SetData");
        }            
    }

    protected void btn_ExecuteClick(object sender, EventArgs e)
    {
        if (!MISAPP.Classify.Service.Utility.IsDate(
            Utility.GetYYYYMM(txtYM.Text)))
        {
            //throw new Exception("日期格式錯誤！");
            lblResult.Text = "日期格式錯誤！";
            return;
        }
        if (rdoTWD.Checked)
        {
            execute();
        }
        else
        { 
            executeCUR();
        }
        
        //Server.Transfer("WMG005_1.aspx");
    }

    /// <summary>
    /// 外幣
    /// </summary>
    protected void executeCUR()
    {
        string ym = Utility.GetYYYYMM(txtYM.Text);
        string flName = "WMGCUR.xls";
        string dlFile = Utility.TemplateToDownload(Server.MapPath("~")
            , flName, "xls");

        //step2 要輸入出到excel Squared_Excel
        //wmg.GetSummaryData(ym, CLB_CUR.GetSelectValue());
        Squared_Excel seq_xls = new Squared_Excel();
        seq_xls.WMG005(new Service_WMG005(), dlFile, ym
            , CLB_CUR.GetSelectValue());
        linkExcel.NavigateUrl = string.Format("~/download/{0}",
           System.IO.Path.GetFileName(dlFile));
        linkExcel.Text = "目前結果";
    }

    protected void execute()
    {
        if (rdo3.Checked) //All 不能在這裏執行
        { return; }

        string ym = Utility.GetYYYYMM(txtYM.Text);
        //Session["Criteria"] = ym;
        string flName = rdo1.Checked ? "WMG005.xls" : "WMG005_1.xls";


        string dlFile = Utility.TemplateToDownload(Server.MapPath("~")
            , flName, "xls");
        Service_WMG005 wmg = new Service_WMG005();
        Service_WMG005.DepositOpt opt = rdo1.Checked ?
            Service_WMG005.DepositOpt.Deposit : 
            Service_WMG005.DepositOpt.Savings;

        wmg.GetSummaryData(ym,opt);
        Squared_Excel seq_xls = new Squared_Excel();
        seq_xls.WMG005(wmg, dlFile, txtYM.Text,opt);
        linkExcel.NavigateUrl = string.Format("~/download/{0}",
            System.IO.Path.GetFileName(dlFile));
        linkExcel.Text = "目前結果";
        seq_xls.ReleaseExcel();
        
    }

    protected void btn_ChartClick(object sender, EventArgs e)
    {
        string ym = Utility.GetYYYYMM(txtYM.Text);
        Service_WMG005 wmg = new Service_WMG005();
        DataTable dt = wmg.GetTrendData(ym);
        chart1.SetChart(wmg.GetChartColumnDict(), wmg.GetChartRow()
            , wmg.GetChartData(getDepositOpt())
            , getChartCaption());
        chart1.CreateChart();
        CLB_Summary.SetData(dt, null);
    }

    protected Service_WMG005.DepositOpt getDepositOpt()
    {
        Service_WMG005.DepositOpt opt = Service_WMG005.DepositOpt.All;
        if (rdo1.Checked)
        {
            opt = Service_WMG005.DepositOpt.Deposit;
        }
        if (rdo2.Checked)
        { 
            opt = Service_WMG005.DepositOpt.Savings;
        }
        return opt;
    }

    protected string getChartCaption()
    {
        string chartCaption = "{0}利率型態走勢圖";
        if (rdo1.Checked)
        {
            chartCaption = string.Format(chartCaption,"定存");
        }
        if (rdo2.Checked)
        {
            chartCaption = string.Format(chartCaption, "定儲");
        }
        if (rdo3.Checked)
        {
            chartCaption = string.Format(chartCaption, "定存+定儲");
        }
        return chartCaption;
    }
}
